﻿//JumbotCms_4.1 Final
using System;
using System.Web;
using System.Data;
using Jumbot.Common;
using JumbotCms.Common;
namespace JumbotCms.WebFile.MTag
{
    public partial class _view_ajax : JumbotCms.API.UserCenter
    {
        private string _operType = string.Empty;
        private string _response = string.Empty;

        protected void Page_Load(object sender, EventArgs e)

        {
            if (!CheckFormUrl())
            {
                Response.Write("提交有误");
                Response.End();
            }

            this._operType = q("oper");
            switch (this._operType)
            {
                case "ajaxCheckPower":
                    ajaxCheckPower();
                    break;
                case "ajaxJoinMTag":
                    ajaxJoinMTag();
                    break;
                case "ajaxQuitMTag":
                    ajaxQuitMTag();
                    break;
                case "ajaxGetList":
                    ajaxGetList();
                    break;
                default:
                    DefaultResponse();
                    break;
            }
            Response.Write(this._response);
        }
        private void DefaultResponse()
        {
            User_Load("", "json");
        }

        private void ajaxCheckPower()
        {
            User_Load("", "json");
            string mtagid = Str2Str(q("mtagid"));
            string ismember = (IsMTagMember(UserId, mtagid)) ? "1" : "0";
            string istalker = (IsMTagTalker(UserId, mtagid)) ? "1" : "0";
            string ismoderator = (IsMTagModerator(UserId, mtagid)) ? "1" : "0";
            string isfounder = (IsMTagFounder(UserId, mtagid)) ? "1" : "0";
            this._response = "{" +
                "ismember :\"" + ismember + "\"," +
                "istalker :\"" + istalker + "\"," +
                "ismoderator :\"" + ismoderator + "\"," +
                "isfounder :\"" + isfounder + "\"" +
                "}";
        }
        private void ajaxJoinMTag()
        {
            User_Load("", "json");
            string tId = f("id");
            doh.Reset();
            doh.ConditionExpress = "mtagid=@mtagid and UserId=" + UserId;
            doh.AddConditionParameter("@mtagid", tId);
            if (doh.Exist("jcms_normal_mtag_member"))
                this._response = JsonResult(0, "你已是群组成员");
            else
            {
                doh.Reset();
                doh.ConditionExpress = "id=@id";
                doh.AddConditionParameter("@id", tId);
                object[] _value = doh.GetValues("jcms_normal_mtag", "Title,UserId,JoinPower");
                string _mtagName = _value[0].ToString();
                string uId = _value[1].ToString();
                int _JoinPower = Str2Int(_value[2].ToString());//0直接加入，1需要审核，2不能加入
                if (_JoinPower == 2)
                    this._response = JsonResult(0, "该群禁止你加入");
                else
                {
                    doh.Reset();
                    doh.AddFieldItem("MTagId", tId);
                    if (_JoinPower == 0)
                        doh.AddFieldItem("Power", 0);
                    else
                        doh.AddFieldItem("Power", -2);
                    doh.AddFieldItem("UserId", UserId);
                    doh.Insert("jcms_normal_mtag_member");
                    //统计加一
                    doh.Reset();
                    doh.ConditionExpress = "id=@id";
                    doh.AddConditionParameter("@id", tId);
                    doh.Add("jcms_normal_mtag", "MemberTotal", 1);
                    if (_JoinPower == 0)
                    {
                        SendNotite("加入群组", "<a href=\"javascript:void(0);\" onclick=\"ShowUserPage(" + UserId + ");\">" + UserName + "</a> 加入了群组 <a href=\""+site.Dir + "mtag/view/index.aspx?mtagid=" + tId + "\">" + _mtagName + "</a>", uId, "mtag");
                        this._response = JsonResult(1, "成功加入");
                    }
                    else
                    {
                        SendNotite("加入群组", "<a href=\"javascript:void(0);\" onclick=\"ShowUserPage(" + UserId + ");\">" + UserName + "</a> 申请加入群组 <a href=\""+site.Dir + "mtag/view/index.aspx?mtagid=" + tId + "\">" + _mtagName + "</a>，需要你审核", uId, "mtag");
                        this._response = JsonResult(1, "已加入，但需要审核");
                    }
                }
            }
        }
        private void ajaxQuitMTag()
        {
            User_Load("", "json");
            string tId = f("id");
            doh.Reset();
            doh.ConditionExpress = "mtagid=@mtagid and UserId=" + UserId;
            doh.AddConditionParameter("@mtagid", tId);
            if (!doh.Exist("jcms_normal_mtag_member"))
                this._response = JsonResult(0, "你不是群组成员");
            else
            {
                doh.Reset();
                doh.ConditionExpress = "id=@id and UserId=" + UserId;
                doh.AddConditionParameter("@id", tId);
                if (doh.Exist("jcms_normal_mtag"))
                    this._response = JsonResult(0, "你是主群主,不能退出");
                else
                {
                    doh.Reset();
                    doh.ConditionExpress = "mtagid=@mtagid and UserId=" + UserId;
                    doh.AddConditionParameter("@mtagid", tId);
                    doh.Delete("jcms_normal_mtag_member");
                    //统计减一
                    doh.Reset();
                    doh.ConditionExpress = "id=@id";
                    doh.AddConditionParameter("@id", tId);
                    doh.Substract("jcms_normal_mtag", "MemberTotal", 1);
                    doh.Reset();
                    doh.ConditionExpress = "id=@id";
                    doh.AddConditionParameter("@id", tId);
                    object[] _value = doh.GetValues("jcms_normal_mtag", "Title,UserId,JoinPower");
                    string _mtagName = _value[0].ToString();
                    string uId = _value[1].ToString();
                    SendNotite("退出群组", "<a href=\"javascript:void(0);\" onclick=\"ShowUserPage(" + UserId + ");\">" + UserName + "</a> 退出了群组 <a href=\""+site.Dir + "mtag/view/index.aspx?mtagid=" + tId + "\">" + _mtagName + "</a>", uId, "mtag");
                    this._response = JsonResult(1, "成功退出");
                }
            }
        }
        private void ajaxGetList()
        {
            User_Load("", "json");
            int page = Int_ThisPage();
            int PSize = Str2Int(q("pagesize"), 10);
            int countNum = 0;
            string sqlStr = "";
            string joinStr = "a.SortId=B.Id";
            string whereStr1 = "A.Id=" + Str2Str(q("mtagid"));
            string whereStr2 = "Id=" + Str2Str(q("mtagid"));
            whereStr1 += " AND A.UserId=" + UserId;
            whereStr2 += " AND UserId=" + UserId;
            doh.Reset();
            doh.ConditionExpress = whereStr2;
            countNum = doh.Count("jcms_normal_mtag");
            sqlStr = Jumbot.Common.staticFunction.GetSql("a.id as mtagid,a.Title as mtagname,a.Img as mtagImg,a.MemberTotal as mtagMemberTotal,a.Proclaim as mtagProclaim,a.JoinPower as mtagJoinPower,a.ViewPower as mtagViewPower,a.CloseApply as mtagCloseApply,b.id as mtagsortid,b.Title as mtagsortname", "jcms_normal_mtag", "jcms_normal_mtag_sort", "Id", PSize, page, "desc", joinStr, whereStr1, whereStr2);
            doh.Reset();
            doh.SqlCmd = sqlStr;
            DataTable dt = doh.GetDataTable();
            this._response = "{result :\"1\"," +
                "returnval :\"操作成功\"," +
                "pagerbar :\"" + Jumbot.Common.HtmlPager.GetPageBar(3, "js", 2, countNum, PSize, page, "javascript:ajaxList(<#page#>);") + "\"," +
                Jumbot.Common.JsonHelp.DtToSON(dt, (PSize * (page - 1))) +
                "}";
            dt.Clear();
            dt.Dispose();
        }
    }
}
